<?php
$connection = mysql_connect("localhost","root","howdy123") or exit("cannot connect to database");

if($connection)
	echo "connected!<br><br>";

$is_complete = true;
$filename = "";
$path = "";

/*	
	iterates through each file passed from the previous
	and makes sure no errors occur.
*/
foreach($_FILES as $file)
{
		if($file["error"] > 0) 
		{
			echo "error uploading file <br>";
			$is_complete = false;
			break;
		}
		else
		{
			$filename = $file["name"];
			$path = $file["tmp_name"];
			echo "File uploaded: ". $filename ." <br>";
		}
	
	print "<br>";
}

/* 
	$is_complete is a boolean value and only is TRUE if
	all needed files are uploaded

	there is no checking for the contents of the files,
	only the last names.
*/

if( $is_complete )
{
	if( mysql_query('CREATE DATABASE db',$connection) )
		echo "database created <br>";
}

$db = mysql_select_db("db",$connection);
if($db)
	echo "connected to schema <br>";
else
	die ("Could not connect to database: ". mysql_error() );

// print "********** creating .data table ********** <br>";


//checks to make sure .data file exists
if( !$_FILES["data"] )
	exit("no data file exists");
if( $_FILES )
{
	// read .data file to %file
	$file = fopen( $_FILES["data"]["tmp_name"],"r") or exit("invalid file!");
}	
Print "library type: data <br>";

$sql ="CREATE TABLE data
(
user_id int, 
item_id int,
rating int,
timestamp int
)";

// submit $sql query
if( mysql_query($sql,$connection) )
	echo "table data created <br>";

// iterates through each line of file and parses it to meet
// the format of the sql query
while( !feof($file))
{
	$line = fgets($file);	// gets next line from file	
	$line = trim($line);	// get rid of white space in the string
	$line = preg_replace('/\s+/', ',', $line);	// replace whitespace with comma
	$sql= "INSERT INTO data 
		(user_id, item_id, rating, timestamp)
		VALUES(".$line.")";
	//echo $sql ."<br>";	
	if( !mysql_query($sql))
		echo "value not added <br>";
}

// print "********** creating .data table ********** <br>";

// repeats the same process of all 3 files.

if( !$_FILES["item"] )
	exit("no .item file exists");
if( $_FILES )
	$file = fopen( $_FILES["item"]["tmp_name"],"r") or exit("invalid .item file!");
	
Print "library type: item <br>";

$sql ="CREATE TABLE item
(
movie_id int PRIMARY KEY,
movie_title text,
release_date date,
video_release_date text,
IMDB_url text,
unknown int,
Action int,
Adventure int,
Animation int,
Childrens int,
Comedy int,
Crime int,
Documentary int,
Drama int,
Fantasy int,
FilmNoir int,
Horror int,
Musical int,
Mystery int,
Romance int,
SciFi int,
Thriller int,
War int,
Western int
)";
if( mysql_query($sql,$connection) )
	echo "table  created <br>";
else
	die("create table failed: ". mysql_error() );

while( !feof($file))
{
	$line = fgets($file);
	$line = '"'.$line. '"';
	$line = str_replace('|','","',$line);
	
	$sql= "INSERT INTO item 
		(movie_id , movie_title, release_date, video_release_date, IMDB_url,
			unknown, Action, Adventure, Animation,Childrens, Comedy, Crime,
			Documentary, Drama, Fantasy, FilmNoir, Horror, Musical, Mystery,
			Romance, SciFi, Thriller, War, Western
		)
		VALUES(".$line.")";
//	echo $sql ."<br>";
	if(!mysql_query($sql,$connection))
		 echo "value not added: ". mysql_error() ."<br>";
}


// print "********** creating .data table ********** <br>";
if( !$_FILES["user"] )
	exit("no data file exists");
if( $_FILES )
	$file = fopen( $_FILES["user"]["tmp_name"],"r") or exit("invalid file!");
	
Print "library type: user <br>";

$sql ="CREATE TABLE user
(
user_id int PRIMARY KEY,
age int,
gender varchar(2),
occupation text,
zipcode int
)";
if( mysql_query($sql,$connection) )
	echo "table user created <br>";

while( !feof($file))
{
	$line = fgets($file);
	$line = '"'.$line.'"';
	$line = str_replace('|','","',$line);
	
	$sql= "INSERT INTO user 
		(user_id, age, gender, occupation, zipcode)
		VALUES(".$line.")";
//	echo $sql . "<br>";
	if( !mysql_query($sql,$connection) )
		die("value not added: ". mysql_error(). "<br>");
}

fclose($file);
mysql_close($connection);

?>
